package pATT.bNEdit.gui;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import pATT.bNEdit.base.Nodo;
import pATT.bNEdit.base.Potencial;


/**
 * <p>Title: BNEdit</p>
 * <p>Description: Adapter for the class ProbCondicional. Allows it to be
* shown and edited inside a JTable </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Martin Blech
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public class PotencialTableModel extends AbstractTableModel {
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private Nodo node;
  private Potencial prob;

  /**
   * Construct a TableModelPotentialAdapter for the given ProbCondicional
   * @param node Nodo
   */
  public PotencialTableModel(Nodo node) {
    this.node = node;
    this.prob = node.getProbabilidadOriginal();
  }

  public int getRowCount() {
    Enumeration enum1 = prob.getDominio().getEstados().elements();
    Integer item = (Integer)enum1.nextElement();
    return item.intValue();
  }

  public int getColumnCount() {
    return prob.tamanio() / getRowCount() + 1;
  }

  public String getColumnName(int columnIndex) {
    if (columnIndex>0) {
      return getStates(columnIndex).toString();
    }
    else
      return "State";
  }

public Class getColumnClass(int columnIndex) {
    if (columnIndex>0)
      return Double.class;
    else
      return String.class;
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex>0) {
      columnIndex--;
      return new Double(prob.get(positionFor(rowIndex, columnIndex)));
    }
    else
      return node.getNombreEstado(rowIndex);
  }

  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if (columnIndex>0) {
      columnIndex--;
      prob.set(positionFor(rowIndex, columnIndex), ( (Double) aValue).doubleValue());
    }
    else {
      Vector states = node.getEstados();
      states.set(rowIndex,aValue);
      node.setEstados(states);
    }
  }

  private int positionFor(int rowIndex, int columnIndex) {
    return (getColumnCount()-1)*rowIndex + columnIndex;
  }

  private Vector getStates(int columnIndex) {
    Vector states = new Vector();
    int size = getColumnCount()-1;
    if (columnIndex>0) {
      columnIndex--;
      Enumeration enumStates = prob.getDominio().getEstados().elements();
      Enumeration enumParents = node.getPadres().elements();
      enumStates.nextElement();
      while (enumStates.hasMoreElements()) {
        int n = ((Integer)enumStates.nextElement()).intValue();
        size /= n;
        int stateIndex = columnIndex / size;
        Nodo parent = (Nodo) enumParents.nextElement();
        states.add(parent.getNombreEstado(stateIndex));
        columnIndex %= size;
      }
    }
    return states;
  }

}
